package com.xiaolin.Thread;

import java.util.concurrent.*;

/**
 * @ClassName TestCallable
 * @Description TODO
 * @Detail Callable1实现多线程
 * @Author MyPC
 * @Date 2020/10/5
 * @Version 1.0
 */
public class TestCallable {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main开始咯");
        // 创建单个线程的线程池
        ExecutorService executorService=Executors.newSingleThreadExecutor();
        CallableDemo c1=new CallableDemo();
        Future<Integer> f1 = executorService.submit(c1);
        System.out.println("hello");
        //获取callable接口的返回值时会阻塞，会等待线程体执行完毕
        System.out.println(f1.get());
        System.out.println("结束了");
        // 关闭线程池
        executorService.shutdown();
    }
}

class CallableDemo implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        for (int i=0;i<50;i++){
            System.out.println("call:"+i+Thread.currentThread());
        }
        Thread.sleep(2000L);
        return 10;
    }
}
